package id.co.rho.scoring.cid.client.mvc.model;

import id.co.rho.scoring.cid.client.command.CommandDispatcherAsync;
import id.co.rho.scoring.cid.client.command.impl.ModelSearchCommandAction;
import id.co.rho.scoring.cid.client.command.impl.ModelSearchCommandAction.SearchType;
import id.co.rho.scoring.cid.client.command.impl.ModelSearchCommandResult;
import id.co.rho.scoring.cid.client.command.impl.TypeModelConfigCommand;
import id.co.rho.scoring.cid.client.command.impl.TypeModelConfigCommand.CommandType;
import id.co.rho.scoring.cid.client.command.impl.TypeModelConfigResult;
import id.co.rho.scoring.cid.client.mvc.AppEvents;
import id.co.rho.scoring.cid.client.security.Credential;

import java.util.List;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.ListStore;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;

public class ModelSearchView extends View{
	
	private ModelSearchUI ui;
	private CommandDispatcherAsync dispatcher;
	private Credential credential;
	private long id;

	@Inject 
	public ModelSearchView(ModelSearchUI ui, Credential credential,
			CommandDispatcherAsync dispatcher) {
		super(null);
		this.ui = ui;
		controller = new ViewController();
		this.dispatcher = dispatcher;
		this.credential = credential;
	}

	@Override
	public void initialize() {
		super.initialize();
		ui.initialize();
		ui.getSearchButton().addSelectionListener(
				new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {
						searchModel();
					}
				});
		
		ui.getResetButton().addSelectionListener(
				new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {
						ui.clearData();
						searchModel();
					}
				});
		
		ui.getGrid().addListener(Events.CellDoubleClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				GridEvent ge = (GridEvent) be;
                AppEvent appEvent = new AppEvent(AppEvents.ScoringModel);
                appEvent.setHistoryEvent(true);
                appEvent.setData("modelId",ge.getModel().get("id"));
				Dispatcher.forwardEvent(appEvent);
			}

		});
	}
	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType().equals(AppEvents.SearchModelEvent)) {
			ui.getFieldName().clear();
			ui.getFieldRevision().clear();
			ui.getModelType().clear();
			ui.show();
			searchModel();
		}		
	}
	
	public  void searchModel() {
		ModelSearchCommandAction action =  new ModelSearchCommandAction();
		action.setName(ui.getFieldName().getValue());
		action.setRevision(ui.getFieldRevision().getValue());		
		action.setType(ui.getModelType().getValue());
		action.setSearchType(SearchType.MAINSEARCH);
		
		dispatcher.execute(action, new AsyncCallback<ModelSearchCommandResult>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(ModelSearchCommandResult result) {
				ui.setModels((List<ModelData>) result.getModels());
			}

		});
		
	}

	private class ViewController extends Controller {
		private ViewController() {
			registerEventTypes(AppEvents.SearchModelEvent);
		}

		@Override
		public void handleEvent(AppEvent event) {
			forwardToView(ModelSearchView.this, event);
		}

	}

}
